package project;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ClashScreen extends javax.swing.JFrame{
private RecordInfo resultRecord;
private boolean stable;
public RecordInfo getResult(){
    return resultRecord;
}
public ClashScreen(final RecordInfo record1, final RecordInfo record2){
super("Resolve Conflicts");
final Container cp = getContentPane();
  //  setSize(new Dimension(800, 580));
    resultRecord = new RecordInfo();
    resultRecord.copyInfo(record1.getDatabase(), record1.getTableName(), record1.getEntryNum());
    stable = true;
    JPanel panel = new JPanel();
    cp.add(panel, BorderLayout.NORTH);
    JPanel bottomPanel = new JPanel();
    cp.add(bottomPanel,BorderLayout.SOUTH);
    //cp.setSize(800,580);
    this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    panel.setBackground(Color.WHITE);
    cp.add(panel);
    panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
    Object[][] attribArray = new Object[record1.getAttributes().size()][2];
    final JList[] lists = new JList[record1.getAttributes().size()];
    int panelWidth = 0;
    panel.setBorder(BorderFactory.createEmptyBorder(0, 20, 10, 10));
    for (int i=0;i<record1.getAttributes().size();i++){
        attribArray[i][0] = record1.getAttribute(i);
        if (!attribArray[i][0].toString().equals(record2.getAttribute(i))){
            attribArray[i][1] = record2.getAttribute(i);
            lists[i] = new JList(attribArray[i]);
        } else {
            attribArray[i][1] = null;
            lists[i] = new JList(attribArray[i]);
            lists[i].setSelectedIndex(0);
        }
        panel.add(lists[i]);
panel.add(Box.createRigidArea(new Dimension(20, 0)));
        panelWidth += (lists[i].WIDTH + 50);

    }
   // JList list = new JList();
setSize(panelWidth,150);
panel.setSize(panelWidth, 75);
bottomPanel.setSize(panelWidth, 100);
JButton commit = new JButton("Commit");
commit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        resultRecord.attributes.clear();
        for (int i=0;i<record1.getAttributes().size();i++){

            if (lists[i].isSelectionEmpty()){
                System.err.println("Attribute "+ (i+1) + " not selected.");
                stable = false;
            } else{
                resultRecord.attributes.add(lists[i].getSelectedValue());
            }
        }
        for (int i=0;i<resultRecord.attributes.size();i++){
            System.out.println(resultRecord.attributes.get(i));
        }
        if (!stable){
            resultRecord = null;
        }
    }
});
bottomPanel.add(commit);
setVisible(true);
}

public static void main(final String args[]) {
    DatabaseInfo d1 = new DatabaseInfo();
    d1.setDatabaseName("numberTrans");d1.setHostName("localhost");d1.setPassword("password");
    d1.setPortNo(3306);d1.setUserName("root");
    RecordInfo r1 = new RecordInfo(d1,23);
        DatabaseInfo d2 = new DatabaseInfo();
    d2.setDatabaseName("numberTrans2");d2.setHostName("localhost");d2.setPassword("password");
    d2.setPortNo(3306);d2.setUserName("root");
    RecordInfo r2 = new RecordInfo(d2,23);
	ClashScreen cs = new ClashScreen(r1,r2);
//cs.setVisible(true);

	}
}
